package Demo1;


import java.util.concurrent.locks.ReentrantLock;

public class T2 {
    public static void main(String[] args) {
        Task task = new Task();
        Thread thread1 = new Thread(task,"A:");
        Thread thread2 = new Thread(task,"B:");
        thread1.start();
        thread2.start();
    }
}

class Task implements Runnable{
    private int tickets = 100;
    private ReentrantLock reentrantLock = new ReentrantLock();//互斥锁   lock 的实现类
    @Override
    public void run() {
        while (true) {
            reentrantLock.lock();//上锁
            try {
                if (tickets == 0){
                    break;
                }
                System.out.println(Thread.currentThread().getName()+"卖出了第"+tickets+"票");
                tickets--;
            } finally {
                reentrantLock.unlock();//释放锁
            }
        }

    }
}
